package org.bot.plugin.web.event;

import org.bot.api.event.DataStorage;
import org.bot.api.event.EventName;
import org.bot.api.util.StringUtil;
import org.bot.api.event.Events;
import org.bot.api.flow.FlowNode;
import org.bot.plugin.web.util.WebApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


/**
 * @author: 沧月
 * @date: 0014 2023/9/14 9:29
 * config   {"cmd":"org.bot.plugin.web.event.WebGoto","arg":"要打开的链接"}
 **/
@Component
@EventName("打开页面")
public class WebGoto implements Events {

    @Autowired
    WebApi webApi;

    @Override
    public Object exec(FlowNode note, DataStorage data) {
        String url;
        if (StringUtil.isNotNull(note.getDataIndex())) {
            if (data.containsKey(note.getDataIndex())) {
                url = data.get(note.getDataIndex()).toString();
            } else {
                throw new RuntimeException("需要打开的URL错误!");
            }
        } else {
            url = note.getArg();
        }
        webApi.goTo(webApi.getWebDriver(note), url);
        return null;
    }
}
